package com.xinqi.modules.course.content.controller.inner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.course.content.convert.CourseContentConvert;
import com.xinqi.modules.course.content.domain.CourseContentEntity;
import com.xinqi.modules.course.content.dto.client.CourseContentClient;
import com.xinqi.modules.course.content.dto.req.CourseContentCreateDTO;
import com.xinqi.modules.course.content.dto.req.CourseContentUpdateDTO;
import com.xinqi.modules.course.content.dto.rsp.CourseContentResultDTO;
import com.xinqi.modules.course.content.manager.CourseContentManager;
import com.xinqi.modules.course.content.service.CourseContentService;
import io.swagger.v3.oas.annotations.Hidden;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 课程内容表内部控制层
 *
 * @author: system
 * @date: 2023/10/25
 */
@Hidden
@Primary
@RestController
@RequiredArgsConstructor
public class CourseContentInnerController implements CourseContentClient {
    private final CourseContentService courseContentService;
    private final CourseContentManager courseContentManager;
    private final CourseContentConvert courseContentConvert;

    @Override
    public R<Boolean> saveContent(CourseContentCreateDTO creat) {
        return R.ok(courseContentManager.save(courseContentConvert.create(creat)));
    }

    @Override
    public R<Boolean> updateById(Long id, CourseContentUpdateDTO update) {
        CourseContentEntity entity = courseContentManager.findById(id);
        if (Objects.isNull(entity)) {
            return R.error("找不到该对象");
        }
        return R.ok(courseContentManager.updateById(courseContentConvert.update(update, entity)));
    }

    @Override
    public R<Boolean> updateBatch(List<CourseContentUpdateDTO> updateList) {
        List<CourseContentEntity> list = updateList.stream().map(courseContentConvert::update).collect(Collectors.toList());
        return R.ok(courseContentManager.updateBatchById(list));

    }

    @Override
    public R<CourseContentResultDTO> selectByContentIdAndType(Long contentId, Integer type) {
        CourseContentEntity entity = courseContentService.selectByContentIdAndType(contentId, type);
        return R.ok(courseContentConvert.convert(entity));
    }

    @Override
    public R<Boolean> saveBatchContent(List<CourseContentCreateDTO> list) {
        List<CourseContentEntity> entityList = new ArrayList<>();
        list.forEach(l -> {
            CourseContentEntity entity = courseContentConvert.create(l);
            entityList.add(entity);
        });
        return R.ok(courseContentManager.saveBatch(entityList, entityList.size()));
    }

    @Override
    public R<CourseContentResultDTO> findContentById(Long contentId) {
        LambdaQueryWrapper<CourseContentEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseContentEntity::getContentId, contentId);
        return R.data(courseContentConvert.convert(courseContentManager.getOne(wrapper)));
    }

    @Override
    public R<List<CourseContentResultDTO>> findContentList(List<Long> ids) {
        LambdaQueryWrapper<CourseContentEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CourseContentEntity::getContentId, ids);
        return R.data(courseContentConvert.convertList(courseContentManager.list(wrapper)));
    }

    @Override
    public R<List<CourseContentResultDTO>> findContentListByIdsAndType(List<Long> contentIds, Integer type) {
        LambdaQueryWrapper<CourseContentEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CourseContentEntity::getContentId, contentIds)
            .eq(CourseContentEntity::getType, type);
        List<CourseContentEntity> list = courseContentManager.list(wrapper);
        return R.ok(list.stream().map(courseContentConvert::convert).collect(Collectors.toList()));
    }

    @Override
    public R<Boolean> removeById(Long contentId) {
        return R.ok(courseContentService.removeById(contentId));
    }

    @Override
    public R<Boolean> deleteCourseContentByIds(List<Long> contentIds, Integer type) {
        return R.ok(courseContentService.deleteCourseContentById(contentIds, type));
    }

    @Override
    public R<Boolean> removeByIds(List<Long> ids) {
        return R.ok(courseContentService.removeByIds(ids));
    }

    @Override
    public R<Boolean> update(Integer status, Integer type, List<Long> contentIdList) {
        LambdaUpdateWrapper<CourseContentEntity> contentWrapper = new LambdaUpdateWrapper<>();
        contentWrapper.set(CourseContentEntity::getStatus, status).eq(CourseContentEntity::getType, type).in(CourseContentEntity::getContentId, contentIdList);
        return R.ok(courseContentService.update(contentWrapper));
    }
}
